package cn.stylefeng.guns.modular.fruits.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fruits.entity.FruitsBasicChannel;
import cn.stylefeng.guns.modular.fruits.mapper.FruitsBasicChannelMapper;
import cn.stylefeng.guns.modular.fruits.model.params.FruitsBasicChannelParam;
import cn.stylefeng.guns.modular.fruits.model.result.FruitsBasicChannelResult;
import  cn.stylefeng.guns.modular.fruits.service.FruitsBasicChannelService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 销售渠道 服务实现类
 */
@Service
public class FruitsBasicChannelServiceImpl extends ServiceImpl<FruitsBasicChannelMapper, FruitsBasicChannel> implements FruitsBasicChannelService {

    @Override
    public void add(FruitsBasicChannelParam param){
        FruitsBasicChannel entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FruitsBasicChannelParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FruitsBasicChannelParam param){
        FruitsBasicChannel oldEntity = getOldEntity(param);
        FruitsBasicChannel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FruitsBasicChannelResult findBySpec(FruitsBasicChannelParam param){
        return null;
    }

    @Override
    public List<FruitsBasicChannelResult> findListBySpec(FruitsBasicChannelParam param,Long operateId){
        return this.baseMapper.findListBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(FruitsBasicChannelParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FruitsBasicChannelParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FruitsBasicChannel getOldEntity(FruitsBasicChannelParam param) {
        return this.getById(getKey(param));
    }

    private FruitsBasicChannel getEntity(FruitsBasicChannelParam param) {
        FruitsBasicChannel entity = new FruitsBasicChannel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
